#include <iostream>
#include <string>
using namespace std;

struct node
{
	string name;
	float salary;
	node* next;  
};

void insert(node* &head, string newname, float newsalary)
{	
	node* newnode = new node;
	newnode->name = newname;
	newnode->salary = newsalary;

    newnode->next = head;
    head = newnode;	
}

void display(node* head)
{
	node* pread = head;
	cout << "The link list is: \n";
	while(pread != NULL)
	{
		cout << "Name: " << pread->name << "  " << "Salary: " << pread->salary << endl;
		pread = pread->next;
	}
	cout << endl;
}

void clear(node* &head)//delete link list function.????. 
{
	node*p;//define to be deleted node.
	while (head != NULL)
	{
		p = head;
		head = head->next;
		delete p;
	}
	cout << "the link list han been deleted!" << endl;
}
//
//Salary as a descending order,exchange the value of the two nodes to achieve sort.
//
void downsort(node* &head)
{
	for (node* pread = head; pread != NULL; pread = pread->next)
	{
		for (node* p = pread->next; p != NULL; p = p->next)
		{
			float temp;//middle variable.
			string tempname;
			if(pread->salary < p->salary)
			{
				temp = p->salary;
				p->salary = pread->salary;
				pread->salary = temp;
				
				tempname = p->name;
				p->name = pread->name;
				pread->name = tempname;
				
			}		
		}	
	}
}

int main()
{
    node* head = NULL;
	insert(head, "Jimmy", 1250.04);
	insert(head, "Tim", 1280.07);
	insert(head, "Rose", 1390.08);
	insert(head, "Tommy", 1490.35);
	insert(head, "Jon", 1320.54);
	display(head);
	
	cout << "Salary as a descending order : \n";
	downsort(head);
	display(head);
	
	clear(head);
	return 0;
}
